/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* removeElements(struct ListNode* head, int val){
       struct ListNode* ans=NULL; 
       struct ListNode* prev=NULL;
       ///1.首个元素就是val 分为prev是NULL还是非NULL的情况
       ///2.继续找
       while(head!=NULL){
           if(head->val==val){
               if(prev==NULL){
                   prev=head;
                   head=head->next;
                   free(prev);
                   prev=NULL;
               }
               else{
                   prev->next=head->next;
                   free(head);
                   head=prev->next;  
               }
           }else{
               if(ans==NULL) ans=head;
               prev=head;
               head=head->next;
           }
       }
       return ans;
}